Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to set one pin of the serial port continuously high using python (or C)? If yes, how?

share|improve this question
Which API are you using to access the serial port? – S.Lott Mar 13 '10 at 15:24

3 Answers

Yes it is possible using pySerial.

share|improve this answer

Using the pyserial methods setRTS(level=True) and setDTR(level=True) you can control the RTS and DTR lines at will. For instance, the following code will toggle the RTS pin of the first serial port. (See the pyserial documentation for the details).

import time
import serial

ser = serial.Serial(0)
ser.setRTS(False)
time.sleep(0.5)
ser.setRTS(True)
time.sleep(0.5)
ser.setRTS(False)
share|improve this answer

If you open the port, the Tx line should go to the voltage high state, and if you never send data it should stay in that state (see here). I've never actually tried this though. You might find you need to enable one of the control lines instead, like CTR.

As for software, TheMachineCharmer's suggestion of pySerial is the most platform independent way of doing it. If you're on Windows, the Win32 API calls are summarized here. Look for CreateFile and (if you need to set control lines) GetCommState and SetCommState. Note that the filename you pass to CreateFile should look like "\\\\.\\COM1". The .NET way of doing is is summarized here. You'll need to construct a SerialPort object, then access its properties to set control lines.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.